/// 通话邀请数据模型
class CallInvite {
  final String fromUserId;
  final String fromUserName;
  final bool isVoiceOnly;
  final String messageId;
  final int timeout; // 超时时间（秒）
  final int timestamp; // 消息时间戳（毫秒）

  CallInvite({
    required this.fromUserId,
    required this.fromUserName,
    required this.isVoiceOnly,
    required this.messageId,
    this.timeout = 30,
    required this.timestamp,
  });

  /// 检查邀请是否已过期
  bool get isExpired {
    final now = DateTime.now().millisecondsSinceEpoch;
    final expireTime = timestamp + (timeout * 1000);
    return now > expireTime;
  }

  /// 获取剩余时间（秒）
  int getRemainingSeconds() {
    final now = DateTime.now().millisecondsSinceEpoch;
    final expireTime = timestamp + (timeout * 1000);
    final remaining = (expireTime - now) ~/ 1000;
    return remaining > 0 ? remaining : 0;
  }

  Map<String, dynamic> toJson() {
    return {
      'fromUserId': fromUserId,
      'fromUserName': fromUserName,
      'isVoiceOnly': isVoiceOnly,
      'messageId': messageId,
      'timeout': timeout,
      'timestamp': timestamp,
    };
  }

  factory CallInvite.fromJson(Map<String, dynamic> json) {
    return CallInvite(
      fromUserId: json['fromUserId'] ?? json['from_user_id'] ?? '',
      fromUserName: json['fromUserName'] ?? json['from_user_name'] ?? '',
      isVoiceOnly: json['isVoiceOnly'] ?? json['is_voice_only'] ?? false,
      messageId: json['messageId'] ?? json['message_id'] ?? '',
      timeout: json['timeout'] ?? 30,
      timestamp: json['timestamp'] ?? json['timestamp_ms'] ?? DateTime.now().millisecondsSinceEpoch,
    );
  }
}

